package com.lx.onbording.vshe.testassignment.core.component;

import org.springframework.stereotype.Component;

import com.lx.onbording.vshe.testassignment.core.domain.Order;
import com.lx.onbording.vshe.testassignment.core.domain.OrderItem;
import com.lx.onbording.vshe.testassignment.core.domain.OrderStatus;

@Component
public class OrderProcessor {
	public Order processOrder(Order order) {
		for (OrderItem item : order.getOrderItems()) {
			if (item.getQuantity() > item.getAvailableQuantity()) {
				throw new RuntimeException(
						"OrderItem could not be processed with quantity = "
								+ item.getQuantity()
								+ ", available quantity =  "
								+ item.getAvailableQuantity());
			}
		}
		order.setStatus(OrderStatus.PROCESSED);
		return order;
	}
}
